package com.carey.yygh.cmn.controller;

import com.carey.yygh.cmn.service.DictService;
import com.carey.yygh.common.result.Result;
import com.carey.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("admin/cmn/dict")
@Api(tags = "数据字典")
// @CrossOrigin  //跨域许可
public class CmnController {
    @Autowired
    private DictService dictService;

    //根据id查子数据
    @ApiOperation("根据id获取子节点")
    @GetMapping("findChildData/{id}")
    public Result finChildData(@PathVariable Long id){
        List<Dict> list = dictService.findChildDate(id);
        return Result.ok(list);
    }

    //导出数据字典
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response){
        dictService.exprotData(response);
    }

    //导入数据字典
    @PostMapping("importData")
    public Result importData(MultipartFile file){
        dictService.importData(file);
        return Result.ok();
    }

    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictCode,@PathVariable("value") String value){
         String dictName = dictService.getName(dictCode,value);
         return dictName;
    }

    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value){
        String dictName = dictService.getName("",value);
        return dictName;
    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(@PathVariable String dictCode){
        List<Dict> list = dictService.finByDictCode(dictCode);
        return Result.ok(list);
    }


}
